package client.controllers;

/*
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 *
 * MovementService.java
 *
 * Steven Louie Apr. 8, 2012
 */
import client.ActinoClient;
import client.models.ActinoPlayer;
import client.models.Direction;
import client.net.MovementAction;
import java.util.ArrayList;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.logging.Level;
import java.util.logging.Logger;

public class MovementService implements Runnable {
    
    private LinkedBlockingQueue<MovementAction> mvtActions;
    private ArrayList<ActinoPlayer> otherPlayers;

    public MovementService() {
        mvtActions = new LinkedBlockingQueue<>();
    }

    public void addMovement(MovementAction action) {
        mvtActions.add(action);
    }

    @Override
    public void run() {
        System.out.println("Starting movement service...");
        while (true) {
            otherPlayers = ActinoClient.getInstance().getOtherPlayers();
            
            // Poll the blocking queue for the next movement. If it is
            // empty, it will wait.
            MovementAction mvt = null;
            try {
                mvt = mvtActions.take();
            } catch (InterruptedException ex) {
                Logger.getLogger(MovementService.class.getName()).log(Level.SEVERE, null, ex);
            }
            if (mvt == null) {
                continue;
            }

            // If a new player is found, add it to the list of other players.
            boolean found = false;
            for (int i = 0; i < otherPlayers.size(); ++i) {
                if (otherPlayers.get(i).name.equals(mvt.name)) {
                    ActinoPlayer p = otherPlayers.get(i);
                    p.imgName = mvt.imgName;
                    p.currentImageIndex = mvt.imgIndex;
                    p.dir = mvt.dir;
                    if (p.dir == Direction.UP) {
                        p.currentImage = p.frames.get("up").get(p.currentImageIndex);
                    } else if (p.dir == Direction.DOWN) {
                        p.currentImage = p.frames.get("down").get(p.currentImageIndex);
                    } else if (p.dir == Direction.RIGHT) {
                        p.currentImage = p.frames.get("right").get(p.currentImageIndex);
                    } else if (p.dir == Direction.LEFT) {
                        p.currentImage = p.frames.get("left").get(p.currentImageIndex);
                    }
                    p.x = mvt.x;
                    p.y = mvt.y;
                    p.px = mvt.px;
                    p.py = mvt.py;
                    found = true;
                }
            }
            if (!found) {
                ActinoPlayer p = new ActinoPlayer(mvt.name, mvt.imgName, mvt.x, mvt.y);
                p.dir = mvt.dir;
                p.currentImageIndex = mvt.imgIndex;
                p.initPCor(ActinoClient.TILE_WIDTH);
                otherPlayers.add(p);
            }

        }
    }
}
